package classrecord.classes.business;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;

import classrecord.classes.database.common.DaoManager;
import classrecord.classes.database.common.IEveryTableDao;
import classrecord.classes.database.studentemailaddresstbl.STUDENTEMAILADDRESSTBLDao;
import classrecord.classes.database.studentemailaddresstbl.STUDENTEMAILADDRESSTBLDomain;
import classrecord.classes.database.studenthomeaddresstbl.STUDENTHOMEADDRESSTBLDao;
import classrecord.classes.database.studenthomeaddresstbl.STUDENTHOMEADDRESSTBLDomain;
import classrecord.classes.database.studentmobilephonenumbertbl.STUDENTMOBILEPHONENUMBERTBLDao;
import classrecord.classes.database.studentmobilephonenumbertbl.STUDENTMOBILEPHONENUMBERTBLDomain;
import classrecord.classes.database.studentnametbl.STUDENTNAMETBLDao;
import classrecord.classes.database.studentnametbl.STUDENTNAMETBLDomain;
import classrecord.classes.database.studentnumbertbl.STUDENTNUMBERTBLDao;
import classrecord.classes.database.studentnumbertbl.STUDENTNUMBERTBLDomain;
import classrecord.classes.database.studenttelephonenumbertbl.STUDENTTELEPHONENUMBERTBLDao;
import classrecord.classes.database.studenttelephonenumbertbl.STUDENTTELEPHONENUMBERTBLDomain;

public class StudentCredentialsDao implements IEveryTableDao {

	private static final long serialVersionUID = -2618973723778307786L;

	private STUDENTNUMBERTBLDomain studentnumbertblDomain;
	private STUDENTNAMETBLDomain studentnametblDomain;

	private STUDENTHOMEADDRESSTBLDomain studenthomeaddresstblDomain;
	private STUDENTEMAILADDRESSTBLDomain studentemailaddresstblDomain;

	private STUDENTTELEPHONENUMBERTBLDomain studenttelephonenumbertblDomain;
	private STUDENTMOBILEPHONENUMBERTBLDomain studentmobilephonenumbertblDomain;

	private Connection connection;

	public StudentCredentialsDao() {
		setConnection(new DaoManager().getConnection());
	}

	public STUDENTNUMBERTBLDomain getStudentnumbertblDomain() {
		return studentnumbertblDomain;
	}

	public void setStudentnumbertblDomain(
			STUDENTNUMBERTBLDomain studentnumbertblDomain) {
		this.studentnumbertblDomain = studentnumbertblDomain;
	}

	public STUDENTNAMETBLDomain getStudentnametblDomain() {
		return studentnametblDomain;
	}

	public void setStudentnametblDomain(
			STUDENTNAMETBLDomain studentnametblDomain) {
		this.studentnametblDomain = studentnametblDomain;
	}

	public STUDENTHOMEADDRESSTBLDomain getStudenthomeaddresstblDomain() {
		return studenthomeaddresstblDomain;
	}

	public void setStudenthomeaddresstblDomain(
			STUDENTHOMEADDRESSTBLDomain studenthomeaddresstblDomain) {
		this.studenthomeaddresstblDomain = studenthomeaddresstblDomain;
	}

	public STUDENTEMAILADDRESSTBLDomain getStudentemailaddresstblDomain() {
		return studentemailaddresstblDomain;
	}

	public void setStudentemailaddresstblDomain(
			STUDENTEMAILADDRESSTBLDomain studentemailaddresstblDomain) {
		this.studentemailaddresstblDomain = studentemailaddresstblDomain;
	}

	public STUDENTTELEPHONENUMBERTBLDomain getStudenttelephonenumbertblDomain() {
		return studenttelephonenumbertblDomain;
	}

	public void setStudenttelephonenumbertblDomain(
			STUDENTTELEPHONENUMBERTBLDomain studenttelephonenumbertblDomain) {
		this.studenttelephonenumbertblDomain = studenttelephonenumbertblDomain;
	}

	public STUDENTMOBILEPHONENUMBERTBLDomain getStudentmobilephonenumbertblDomain() {
		return studentmobilephonenumbertblDomain;
	}

	public void setStudentmobilephonenumbertblDomain(
			STUDENTMOBILEPHONENUMBERTBLDomain studentmobilephonenumbertblDomain) {
		this.studentmobilephonenumbertblDomain = studentmobilephonenumbertblDomain;
	}

	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	@Override
	public boolean add() {
		boolean wasInserted = true;
		
		wasInserted = insertIntoStudentNumberTbl(wasInserted);
		wasInserted = insertIntoStudentNametbl(wasInserted);
		wasInserted = insertIntoStudentHomeAddressTbl(wasInserted);
		wasInserted = insertIntoStudentEmailAddressTbl(wasInserted);
		wasInserted = insertIntoStudentTelephoneNumberTbl(wasInserted);
		wasInserted = insertIntoStudentMobilePhoneNumberTbl(wasInserted);
		
		if (wasInserted) {
			DbUtils.commitAndCloseQuietly(connection);
		
		} else {
			DbUtils.rollbackAndCloseQuietly(connection);
		
		}
		
		System.out.println("wasInserted: " + wasInserted);
		return wasInserted;
	}

	private boolean insertIntoStudentMobilePhoneNumberTbl(boolean wasInserted) {
		STUDENTMOBILEPHONENUMBERTBLDao studentmobilephonenumbertblDao = new STUDENTMOBILEPHONENUMBERTBLDao();
		studentmobilephonenumbertblDao.setStudentmobilephonenumbertblDomain(getStudentmobilephonenumbertblDomain());
		studentmobilephonenumbertblDao.setConnection(connection);
		boolean wasInsertedInSMPNTD = studentmobilephonenumbertblDao.add();
		System.out.println("wasInsertedInSMPNTD: " + wasInsertedInSMPNTD);
		wasInserted = wasInserted & wasInsertedInSMPNTD;
		System.out.println("wasInserted: " + wasInserted);
		return wasInserted;
	}

	private boolean insertIntoStudentTelephoneNumberTbl(boolean wasInserted) {
		STUDENTTELEPHONENUMBERTBLDao studenttelephonenumbertblDao = new STUDENTTELEPHONENUMBERTBLDao();
		studenttelephonenumbertblDao.setStudenttelephonenumbertblDomain(getStudenttelephonenumbertblDomain());
		studenttelephonenumbertblDao.setConnection(connection);
		boolean wasInsertedInSTNTD = studenttelephonenumbertblDao.add();
		System.out.println("wasInsertedInSTNTD: " + wasInsertedInSTNTD);
		wasInserted = wasInserted & wasInsertedInSTNTD;
		System.out.println("wasInserted: " + wasInserted);
		return wasInserted;
	}

	private boolean insertIntoStudentEmailAddressTbl(boolean wasInserted) {
		STUDENTEMAILADDRESSTBLDao studentemailaddresstblDao = new STUDENTEMAILADDRESSTBLDao();
		studentemailaddresstblDao.setStudentemailaddresstblDomain(getStudentemailaddresstblDomain());
		studentemailaddresstblDao.setConnection(connection);
		boolean wasInsertedInSEATD = studentemailaddresstblDao.add();
		System.out.println("wasInsertedInSEATD: " + wasInsertedInSEATD);
		wasInserted = wasInserted & wasInsertedInSEATD;
		System.out.println("wasInserted: " + wasInserted);
		return wasInserted;
	}

	private boolean insertIntoStudentHomeAddressTbl(boolean wasInserted) {
		STUDENTHOMEADDRESSTBLDao studenthomeaddresstblDao = new STUDENTHOMEADDRESSTBLDao();
		studenthomeaddresstblDao.setStudenthomeaddresstblDomain(getStudenthomeaddresstblDomain());
		studenthomeaddresstblDao.setConnection(connection);
		boolean wasInsertedInSHATD = studenthomeaddresstblDao.add();
		System.out.println("wasInsertedInSHATD: " + wasInsertedInSHATD);
		wasInserted = wasInserted & wasInsertedInSHATD;
		System.out.println("wasInserted: " + wasInserted);
		return wasInserted;
	}

	private boolean insertIntoStudentNametbl(boolean wasInserted) {
		STUDENTNAMETBLDao studentnametblDao = new STUDENTNAMETBLDao();
		studentnametblDao.setStudentnametblDomain(getStudentnametblDomain());
		studentnametblDao.setConnection(connection);
		boolean wasInsertedInSNamTD = studentnametblDao.add();
		System.out.println("wasInsertedInSNamTD: " + wasInsertedInSNamTD);
		wasInserted = wasInserted & wasInsertedInSNamTD;
		System.out.println("wasInserted: " + wasInserted);
		return wasInserted;
	}

	private boolean insertIntoStudentNumberTbl(boolean wasInserted) {
		STUDENTNUMBERTBLDao studentnumbertblDao = new STUDENTNUMBERTBLDao();
		studentnumbertblDao.setStudentnumbertblDomain(getStudentnumbertblDomain());
		studentnumbertblDao.setConnection(connection);
		boolean wasInsertedInSNumTD = studentnumbertblDao.add();
		System.out.println("wasInsertedInSNumTD: " + wasInsertedInSNumTD);
		wasInserted = wasInserted & wasInsertedInSNumTD;
		System.out.println("wasInserted: " + wasInserted);
		return wasInserted;
	}

	@Override
	public boolean update() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean physicalDelete() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean logicalDelete() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getPhysicalRecordCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getLogicalRecordCount() {
		// TODO Auto-generated method stub
		return 0;
	}

}
